import { Variant, Variants } from "framer-motion";

interface AnimationParams {
  duration?: number;
  delay?: number;
}

export const getAnimationVariants = (
  introType?: string,
  introDuration: number = 1,
  loopType?: string,
  outroType?: string,
  outroDuration: number = 1,
): Variants => {
  const variants: Variants = {};

  // Initial states
  const initialStates: { [key: string]: Variant } = {
    渐显: { opacity: 0 },
    左飞入: { opacity: 0, x: "-100%" },
    右飞入: { opacity: 0, x: "100%" },
    上飞入: { opacity: 0, y: "-100%" },
    下飞入: { opacity: 0, y: "100%" },
    放大: { opacity: 0, scale: 0.5 },
    缩小: { opacity: 0, scale: 1.5 },
  };

  // Animate states (for intro)
  const animateStates: { [key: string]: Variant } = {
    渐显: { opacity: 1 },
    左飞入: { opacity: 1, x: 0 },
    右飞入: { opacity: 1, x: 0 },
    上飞入: { opacity: 1, y: 0 },
    下飞入: { opacity: 1, y: 0 },
    放大: { opacity: 1, scale: 1 },
    缩小: { opacity: 1, scale: 1 },
  };

  // Loop animations
  const loopStates: { [key: string]: Variant } = {
    跳动: {
      scale: [1, 1.05, 1],
      transition: { duration: 0.8, repeat: Infinity, ease: "easeInOut" },
    },
    闪烁: {
      opacity: [1, 0.5, 1],
      transition: { duration: 1, repeat: Infinity, ease: "easeInOut" },
    },
    呼吸: {
      scale: [1, 1.02, 1],
      opacity: [1, 0.8, 1],
      transition: { duration: 1.5, repeat: Infinity, ease: "easeInOut" },
    },
  };

  // Exit states (for outro)
  const exitStates: { [key: string]: Variant } = {
    渐隐: { opacity: 0 },
    左飞出: { opacity: 0, x: "-100%" },
    右飞出: { opacity: 0, x: "100%" },
    上飞出: { opacity: 0, y: "-100%" },
    下飞出: { opacity: 0, y: "100%" },
    缩小消失: { opacity: 0, scale: 0.5 },
    放大消失: { opacity: 0, scale: 1.5 },
  };

  let currentAnimateState: Variant = { opacity: 1 }; // Default visible state

  if (introType && initialStates[introType] && animateStates[introType]) {
    variants.initial = initialStates[introType];
    currentAnimateState = {
      ...animateStates[introType],
      transition: { duration: introDuration },
    };
  } else {
    variants.initial = { opacity: 1 }; // Default visible if no intro
  }

  if (loopType && loopStates[loopType]) {
    // Combine loop with intro's end state if intro exists, or apply directly
    const loopTransition = (loopStates[loopType] as any).transition;
    const loopProperties = { ...loopStates[loopType] };
    delete (loopProperties as any).transition;

    currentAnimateState = {
      ...currentAnimateState, // from intro or default
      ...loopProperties, // loop properties
      transition: {
        // combined or specific loop transition
        ...currentAnimateState.transition,
        ...loopTransition,
      },
    };
  }

  variants.animate = currentAnimateState;

  if (outroType && exitStates[outroType]) {
    variants.exit = {
      ...exitStates[outroType],
      transition: { duration: outroDuration },
    };
  }

  return variants;
};
